package response

import (
    "github.com/gin-gonic/gin"
    "net/http"
    "time"
)

type ApiSuccess struct {
    Code    int         `json:"code" example:"200"`
    Message string      `json:"message" example:"请求错误提示信息"`
    Data    interface{} `json:"data"`
}

type ApiError struct {
    Code    int         `json:"code" example:"400"`
    Message string      `json:"message" example:"请求错误提示信息"`
    Data    interface{} `json:"data"`
}
//
//type response struct {
//    ApiSuccess
//    ApiError
//}
//
//var Response = new(response)

// Success 返回成功
func Success(c *gin.Context, code int, data interface{}) {
    c.JSON(http.StatusOK, map[string]interface{}{
        "code":    code, // 自定义code
        "message": "请求成功",  // message
        "data":    data, // 数据
        "ts": time.Now().Unix(),
    })
    c.Abort()
    return
}

// Err 返回失败
func Err(c *gin.Context, httpCode int, code int, msg string) {
    c.JSON(httpCode, map[string]interface{}{
        "code":    code,
        "message": msg,
        "data":    "",
        "ts": time.Now().Unix(),
    })
    c.Abort()
    return
}

// Success 返回成功html
func SuccessHtml(c *gin.Context, htmlTemplate string, data interface{}) {
    c.HTML(http.StatusOK, htmlTemplate, data)
    return
}

// Success 返回成功html
func FailHtml(c *gin.Context, data interface{}) {
    c.HTML(http.StatusBadRequest, "error.html", data)
    return
}
